package com.kin207.netty.listener.server.handle;

import java.util.Collection;
import java.util.List;

import com.kin207.netty.context.RequestContext;
import com.kin207.netty.handle.IMessageHandle;
import com.kin207.netty.listener.server.InstanceServerListener;
import com.kin207.netty.listener.server.handle.vo.ServerHelpReqVo;
import com.kin207.netty.msg.parse.FieldMessageParamIndex;
import com.kin207.netty.msg.parse.FieldMessageParamUtil;
import com.kin207.netty.server.IServer;

public class ServerHelpMessageHandle implements IMessageHandle<ServerHelpReqVo> {

	public String name() {
		return "/server/help";
	}

	public Class paramClass() {
		return ServerHelpReqVo.class;
	}

	public void handle(ServerHelpReqVo reqVo) {
		RequestContext req = RequestContext.curr.get();
		
		Collection<IServer> servers = req.getServer().getServerManager().getServers().values();
		StringBuffer msg = new StringBuffer();
		for(IServer item : servers){
			if(String.valueOf(item.hashCode()).equals(reqVo.getPid())){
				FieldMessageParamUtil paramUtil = new FieldMessageParamUtil();
				Collection<IMessageHandle> mhs = item.getMessageHandleAdapter().getMessageHandles();
				for(IMessageHandle mh : mhs){
					msg.append(mh.name());
					if(mh.paramClass()!=null){
						List<FieldMessageParamIndex> fields = paramUtil.getMessageParamFields(mh.paramClass());
						for(FieldMessageParamIndex param : fields){
							msg.append(" {").append(param.getField().getName()).append("}");
						}
					}
					msg.append("\n");
				}
			}
		}
		req.wirte(msg.toString());
		
	}

}
